#include "ASMobileComponent.h"

NS_AS_BEGIN

ASComponentNameInstance(ASMobileComponent);

ASMobileComponent::ASMobileComponent()
:_mobileControler(nullptr)
{

}

ASMobileComponent::~ASMobileComponent()
{
}


ASMobileControler::ASMobileControler()
{
}

ASMobileControler::~ASMobileControler()
{
}

void ASMobileControler::addMobile(ASEntity* pMobile,ASMobileComponent* pMobileComponent)
{
	if (pMobile&&pMobileComponent)
	{
		_vMobiles.push_back(pMobile);
		if (pMobileComponent->getMobileControler()==nullptr)
		{
			pMobileComponent->setMobileControler(this);
			pMobileComponent->_SignalDestory.connect(this,&ASMobileControler::removeByMobileObj);
		}
		else
		{
			ASAssert(0,"ASMobileControler::addMobile has contain this ");
		}
	}
}

void ASMobileControler::addMobile(ASMobileComponent* pMobile)
{
	if (pMobile&&pMobile->getHolder())
	{
		if (pMobile->getMobileControler()==nullptr)
		{
			pMobile->setMobileControler(this);
			pMobile->_SignalDestory.connect(this,&ASMobileControler::removeByMobileObj);
			_vMobiles.push_back(pMobile->getHolder());
		}
		else
		{
			ASAssert(0,"ASMobileControler::addMobile has contain this ");
		}
	}
	else
	{
		ASAssert(0,"ASMobileControler::addMobile add entity is null || component is null");
	}
}

void ASMobileControler::addObstacle(ASEntity* pObstacle)
{
	if (pObstacle)
	{
#ifdef ASDEBUG
		for (std::vector<ASEntity*>::iterator it=_vObstacles.begin();it!=_vObstacles.end();it++)
		{
			if (pObstacle==*it)
			{
				ASAssert(0,"ASMobileControler::addObstacle has contain this");	
			}
		}
#endif
		pObstacle->_SignalDestory.connect(this,&ASMobileControler::removeByObstacleObj);
		_vObstacles.push_back(pObstacle);
	}
	else
	{
		ASAssert(0,"ASMobileControler::addObstacle add entity is null");
	}
}

void ASMobileControler::removeObstacle(ASEntity* pObstacle)
{
	if (pObstacle)
	{
		for (std::vector<ASEntity*>::iterator it=_vObstacles.begin();it!=_vObstacles.end();it++)
		{
			if (pObstacle==*it)
			{
				(*it)->_SignalDestory.disconnect(this);
				_vObstacles.erase(it);
				break;
			}
		}
	}
}

void ASMobileControler::removeMobile(ASMobileComponent* pMobile)
{
	ASEntity* pEntity = pMobile->getHolder();
	if (pEntity)
	{
		for (std::vector<ASEntity*>::iterator it=_vMobiles.begin();it!=_vMobiles.end();it++)
		{
			if (pEntity==*it)
			{
				pMobile->_SignalDestory.disconnect(this);
				pMobile->setMobileControler(nullptr);
				_vMobiles.erase(it);
				break;
			}
		}
	}
}

void ASMobileControler::tagMobileWithinViewRange(ASEntity* pEntity,double radius)
{
	for (std::vector<ASEntity*>::iterator it=_vMobiles.begin();it!=_vMobiles.end();it++)
	{
		ASEntity* pHolder = *it;
		if (pHolder)
		{
			pHolder->unTag();
			if (pHolder!=pEntity)
			{
				Vector2D to = pHolder->getPosition() - pEntity->getPosition();
				double range = radius + pHolder->getBoundRadius();
				if ( to.LengthSq() < range*range )
				{
					pHolder->tag();
				}
			}
		}
	}
}
void ASMobileControler::tagObstacleWithinViewRange(ASEntity* pEntity,double radius)
{
	for (std::vector<ASEntity*>::iterator it=_vObstacles.begin();it!=_vObstacles.end();it++)
	{
		ASEntity* pHolder = *it;
		pHolder->unTag();
		if (pHolder!=pEntity)
		{
			Vector2D to = pHolder->getPosition() - pEntity->getPosition();
			double range = radius + pHolder->getBoundRadius();
			if ( to.LengthSq() < range*range )
			{
				pHolder->tag();
			}
		}
	}
}

NS_AS_END